package com.ss.ttvideoengine.b;

import com.ss.ttvideoengine.Resolution;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public final class VideoDataContainer {
    public VideoInfo mVideoInfo;
    public List<VideoAdInfo> mVideoAdInfoList;
    public LiveInfo mLiveInfo;

    public VideoDataContainer() {
        this.mVideoInfo = new VideoInfo();
        this.mVideoAdInfoList = new ArrayList();
        this.mLiveInfo = new LiveInfo();
    }

    public final void initWithJO(JSONObject jo) {
        if (jo == null) {
            return;
        }
        JSONObject videoInfoJO = jo.optJSONObject("video_info");
        if (videoInfoJO != null) {
            this.mVideoInfo.initWithJO(videoInfoJO.optJSONObject("data"));
        }
        JSONArray videoAdListJA = jo.optJSONArray("video_ad_list");
        if (videoAdListJA != null && videoAdListJA.length() > 0) {
            for (int i = 0; i < videoAdListJA.length(); i++) {
                try {
                    VideoAdInfo videoAdInfo = new VideoAdInfo();
                    JSONObject videoAdJO = videoAdListJA.getJSONObject(i);
                    JSONObject videoListJO = videoAdJO.optJSONObject("video_list");
                    try {
                        if (videoListJO.has("video_1")) {
                            videoAdInfo.mVideoItem1 = new VideoItem();
                            videoAdInfo.mVideoItem1.initWithJO(videoListJO.getJSONObject("video_1"));
                        }

                        if (videoListJO.has("video_2")) {
                            videoAdInfo.mVideoItem2 = new VideoItem();
                            videoAdInfo.mVideoItem2.initWithJO(videoListJO.getJSONObject("video_2"));
                        }

                        if (videoListJO.has("video_3")) {
                            videoAdInfo.mVideoItem3 = new VideoItem();
                            videoAdInfo.mVideoItem3.initWithJO(videoListJO.getJSONObject("video_3"));
                        }
                    } catch (JSONException jsonException) {
                        jsonException.printStackTrace();
                    }
                    videoAdInfo.id = videoAdJO.optLong("id");
                    videoAdInfo.logExtra = videoAdJO.optString("log_extra");
                    videoAdInfo.skipTime = videoAdJO.optInt("skip_time");
                    videoAdInfo.patchPosition = videoAdJO.optInt("patch_position");
                    videoAdInfo.duration = videoAdJO.optInt("duration");
                    videoAdInfo.externalUrl = videoAdJO.optString("external_url");

                    JSONArray playTrackUrlListJA = videoAdJO.getJSONArray("play_track_url_list");
                    if (playTrackUrlListJA != null) {
                        videoAdInfo.playTrackUrlList = VideoAdInfo.extractList(playTrackUrlListJA, new String[1]);
                    }
                    JSONArray playoverTrackUrlListJA = videoAdJO.getJSONArray("playover_track_url_list");
                    if (playoverTrackUrlListJA != null) {
                        VideoAdInfo.extractList(playoverTrackUrlListJA, new String[1]);
                    }
                    JSONArray actionTrackUrlListJO = videoAdJO.getJSONArray("action_track_url_list");
                    if (actionTrackUrlListJO != null) {
                        VideoAdInfo.extractList(actionTrackUrlListJO, new String[1]);
                    }
                    this.mVideoAdInfoList.add(videoAdInfo);
                } catch (Exception exception) {
                }
            }
        }
        JSONObject liveInfoJO2 = jo.optJSONObject("live_info");
        if (liveInfoJO2 == null) {
            return;
        }
        JSONObject dataJO = liveInfoJO2.optJSONObject("data");
        LiveInfo liveInfo = this.mLiveInfo;
        if (dataJO == null) {
            return;
        }
        JSONObject liveInfoJO = dataJO.optJSONObject("live_info");
        try {
            if (liveInfoJO.has("live_0")) {
                liveInfo.mLiveItem0 = new LiveItem();
                liveInfo.mLiveItem0.initWithJO(liveInfoJO.getJSONObject("live_0"));
            }

            if (liveInfoJO.has("live_1")) {
                liveInfo.mLiveItem1 = new LiveItem();
                liveInfo.mLiveItem1.initWithJO(liveInfoJO.getJSONObject("live_1"));
            }

            liveInfoJO.optInt("backup_status");
            liveInfo.liveStatus = liveInfoJO.optInt("live_status");
            liveInfoJO.optInt("status");
            liveInfoJO.optLong("start_time");
            liveInfoJO.optLong("end_time");
        } catch (JSONException jsonException) {
            jsonException.printStackTrace();
        }
        dataJO.optString("user_id");
        dataJO.optString("live_id");
        return;
    }

    public final String[] getSelectedResolutionVideoUrls(Resolution resolution) {
        if (this.mVideoInfo != null) {
            VideoItem videoItem = this.mVideoInfo.selectDefinition(resolution.toString());
            if (videoItem != null) {
                return videoItem.videoUrls;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}

